package com.pig.utils;


import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;


public class Utils {
    //获取IP地址
    public static String getIpAddress(HttpServletRequest request) {

        String ipAddress = request.getHeader("x-forwarded-for");

        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknow".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();

            if ("127.0.0.1".equals(ipAddress) || "0:0:0:0:0:0:0:1".equals(ipAddress)) {
                //根据网卡获取本机配置的IP地址
                InetAddress inetAddress = null;
                try {
                    inetAddress = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                ipAddress = inetAddress.getHostAddress();
            }
        }

        //对于通过多个代理的情况，第一个IP为客户端真实的IP地址，多个IP按照','分割
        if (null != ipAddress && ipAddress.length() > 15) {
            //"***.***.***.***".length() = 15
            if (ipAddress.indexOf(",") > 0) {
                ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
            }
        }

        return ipAddress;
    }

    //
    public static boolean isEmpty(String obj) {
        if (obj == null || obj.trim().length() < 1) {
            return false;
        }
        return true;
    }

    //
    //获取当前时间-sql
    public static Timestamp getNowTime() {
        return new Timestamp(System.currentTimeMillis());
    }

    //
    public static String getNowTimes() {
        return System.currentTimeMillis() + "";
    }

    public static String getIp() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ip = (String) request.getSession().getAttribute("ip");
        if (null != ip) {
            return ip;
        }
        else{
            return getIpAddress(request);
        }
    }

//    public static User getAdmin() {
//        User admin = (User) ServletActionContext.getRequest().getSession().getAttribute("Administrator");
//        if (null != admin) {
//            return admin;
//        } else {
//            admin = new User();
//            admin.setUser_id(123);
//            return admin;
//        }
//    }
//
//    /**
//     * 获取服务器内存状态
//     *
//     * @return 内存
//     */
//    public static String getMemory() {
//        OperatingSystemMXBean osmb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
//        long all = osmb.getTotalPhysicalMemorySize() / 1024 / 1024;
//        long free = osmb.getFreePhysicalMemorySize() / 1024 / 1024;
//        return (all - free) + "MB / " + all + "MB";
//    }
//

    /**
     * 格式化时间--小时
     *
     * @param start 时间
     * @param end   时间
     * @return 小时
     */
    public static double getMinutes(long start, long end) {
        double time = (end - start) / 10;
        int d = (int) (time / 60 + 0.5f);
        double e = (double) (d) / 100;
        DecimalFormat df = new DecimalFormat("######0.00");
        return Double.parseDouble(df.format(e));
    }
//
///*    public static void main(String[] a){
//        double hours = getHours(System.currentTimeMillis(), System.currentTimeMillis() + 12660000);
//        System.out.println(hours);
//    }*/
//

    /**
     * 格式化时间--分钟
     *
     * @param start 时间
     * @param end   时间
     * @return 分钟
     */
    public static double getHours(long start, long end) {
        double time = (end - start) / 10;
        int d = (int) (time / 60 / 60 + 0.5f);
        double e = (double) (d) / 100;
        DecimalFormat df = new DecimalFormat("######0.00");
        return Double.parseDouble(df.format(e));
    }
//
//    /**
//     * 格式化时间
//     *
//     * @param time 时间
//     * @return 格式化时间
//     */
//    public static double Min2H(double time) {
//        return 0;
//    }
//
//    /**
//     * 格式化时间
//     *
//     * @param time 时间
//     * @return 格式化时间
//     */
//    public static double Hour2M(double time) {
//        return 0;
//    }
//

    /**
     * 格式化时间
     *
     * @param time 时间
     * @return 时间
     */
    public static long formatTime(String time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm", Locale.CHINA);
        try {
            Date date = format.parse(time);
            return date.getTime();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return 0;
    }


}
